﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CashOrCard.Models;
using System.Reflection;
using System.ComponentModel;

namespace CashOrCard.Helper
{
    public enum EnumUserType
    {
        Customer,
        BusinessOwner,
        Admin,
        Other
    }
    public class Common
    {
        public static EnumUserType GetUserType(string userType)
        {
            EnumUserType enumUserType = EnumUserType.Other;
            switch (userType)
            {
                case "Customer":
                    enumUserType = EnumUserType.Customer;
                    break;
                case "Business Owner":
                    enumUserType = EnumUserType.BusinessOwner;
                    break;
                case "Admin":
                    enumUserType = EnumUserType.Admin;
                    break;
            }

            return enumUserType;

        }

        public static decimal SiteVisitChargeAmount
        {
            get
            {
                return decimal.Parse(System.Configuration.ConfigurationManager.AppSettings["SiteVisitChargeAmount"]);
            }
        }

        public static List<SelectListItem> GetRoleList()
        {
            using (CashOrCardContext db = new CashOrCardContext())
            {
                string selectedAccountType = string.Empty;
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["type"]))
                {
                    selectedAccountType = HttpContext.Current.Request.QueryString["type"];
                }
                //get role list
                List<Role> roleList = db.Roles.Where(x => x.IsAdmin == null || x.IsAdmin == false).ToList<Role>();

                //Add role to select list item object
                List<SelectListItem> deptSelectItems = new List<SelectListItem>();
                foreach (Role r in roleList)
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = r.RoleName;
                    item.Value = r.RoleId.ToString();
                    if (!string.IsNullOrEmpty(selectedAccountType))
                    {
                        if (item.Text.ToLower().Contains(selectedAccountType.ToLower()))
                        {
                            item.Selected = true;
                        }
                    }

                    deptSelectItems.Add(item);
                }

                return deptSelectItems;
            }

        }

        public static List<SelectListItem> GetSiteTypeList(int category = 0)
        {
            using (CashOrCardContext db = new CashOrCardContext())
            {
                List<SiteType> typeList = db.SiteTypes.ToList<SiteType>();

                //Add role to select list item object
                List<SelectListItem> items = new List<SelectListItem>();

                foreach (SiteType st in typeList)
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = st.SiteTypeName;
                    item.Value = st.SiteTypeId.ToString();
                    if (st.SiteTypeId.Equals(category))
                    {
                        item.Selected = true;
                    }
                    items.Add(item);
                }
                items.Insert(0, new SelectListItem { Text = "All", Value = "0" });

                return items;
            }
        }

        public static string CurrentUserName
        {
            get
            {
                using (CashOrCardContext db = new CashOrCardContext())
                {

                    string email = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(email))
                    {
                        User curUser = db.Users.FirstOrDefault(x => x.Email == email && x.IsEnable);
                        return string.Format("{0} {1}", curUser.FirstName, curUser.LastName);
                    }
                    else
                    {
                        return string.Empty;
                    }
                }

            }
        }

        public static bool IsAdmin
        {
            get
            {
                using (CashOrCardContext db = new CashOrCardContext())
                {
                    string email = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(email))
                    {
                        User curUser = db.Users.FirstOrDefault(x => x.Email == email && x.IsEnable);
                        return curUser.Role.RoleName.ToLower() == "admin";
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        public static string CurrentUserEamil
        {
            get
            {
                string email = HttpContext.Current.User.Identity.Name;

                if (!string.IsNullOrEmpty(email))
                {
                    return email;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public static User CurrentUser
        {
            get
            {
                using (CashOrCardContext db = new CashOrCardContext())
                {

                    string email = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(email))
                    {
                        User curUser = db.Users.FirstOrDefault(x => x.Email == email && x.IsEnable);
                        return curUser;
                    }
                    else
                    {
                        return null;
                    }
                }

            }
        }

        public static bool isApproved
        {
            get
            {
                using (CashOrCardContext db = new CashOrCardContext())
                {

                    string email = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(email))
                    {
                        User curUser = db.Users.FirstOrDefault(x => x.Email == email && x.IsEnable);
                        return curUser.IsApproved;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
        }

        public static IEnumerable<dynamic> GetEnums(Type enumType)
        {
            var fields = enumType.GetFields(
                BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public
            );
            var values = Enum.GetValues(enumType).OfType<dynamic>();
            var items =
                from value in values
                from field in fields
                let descriptionAttribute = field
                    .GetCustomAttributes(
                        typeof(DescriptionAttribute), true
                    )
                    .OfType<DescriptionAttribute>()
                    .FirstOrDefault()
                let description = (descriptionAttribute != null)
                    ? descriptionAttribute.Description
                    : value.ToString()
                where value.ToString() == field.Name
                select new { Id = value, Name = description };
            return items;
        }
    }
}